﻿using OperationPlugins.Handlers;
using OperationPlugins.Mvc;
using OperationPlugins.Mvc.Filters;
using OperationPlugins.Mvc.Handlers;
using OperationPlugins.WebApp.Filters;
using OperationPlugins.WebApp.Handlers;
using OperationPlugins.WebApp.ViewModes;
using PerpetuumSoft.Knockout;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OperationPlugins.WebApp.Controllers
{
    [UseActionMethodHandlersForAllActions(ActionMethodMoment.OnActionExecuting)]
    public partial class AdminController : KnockoutController, IViewModelResourceProvider
    {

        #region Common Functionalities

        private TViewModel CreateViewModel<TViewModel>() where TViewModel : ViewModelBase, new()
        {
            TViewModel vm = new TViewModel();
            ViewModelResourceProviderBinder.Bind(this, vm);
            return vm;
        }

        protected internal JsonResult Json(ViewModelBase vm)
        {
            ViewModelResourceProviderBinder.Unbind(vm);
            return base.Json(vm);
        }

        #endregion

        #region View: Applications

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        public ActionResult Applications()
        {
            ApplicationsViewModel vm = CreateViewModel<ApplicationsViewModel>();
            vm.Load();
            return View(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult StartApplicationServer(ApplicationsViewModel vm)
        {
            vm.StartApplicationServer();
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult StopApplicationServer(ApplicationsViewModel vm)
        {
            vm.StopApplicationServer();
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult RecycleInstalledApplications(ApplicationsViewModel vm)
        {
            vm.RecycleInstalledApplications();
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult ApplicationPreferences(ApplicationsViewModel vm, int index)
        {
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult StartApplication(ApplicationsViewModel vm, int index)
        {
            vm.StartApplication(index);
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult StopApplication(ApplicationsViewModel vm, int index)
        {
            vm.StopApplication(index);
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult RecycleApplication(ApplicationsViewModel vm, int index)
        {
            vm.RecycleApplication(index);
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult UninstallApplication(ApplicationsViewModel vm, int index)
        {
            vm.UninstallApplication(index);
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult InstallNewApplication(ApplicationsViewModel vm)
        {
            vm.RedirectTo(Url.Action("Install"));
            return Json(vm);
        }

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        [HttpPost]
        public ActionResult ExportInstallation(ApplicationsViewModel vm)
        {
            vm.RedirectTo(Url.Action("Export"));
            return Json(vm);
        }

        #endregion

        #region View: Install

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        public ActionResult Install()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region View: Export

        [ActiveLayoutMenu(CurrentActionUnderApplications = true)]
        public ActionResult Export()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region View: EventLogs

        [ActiveLayoutMenu(CurrentActionUnderEventLogs = true)]
        public ActionResult EventLogs()
        {
            return View();
        }

        #endregion

        #region View: Preferences

        [ActiveLayoutMenu(CurrentActionUnderPreferences = true)]
        public ActionResult Preferences()
        {
            return View();
        }

        #endregion

    }

}